package com.rc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rc.entity.User;
import com.rc.entity.web.PageParam;
import com.rc.entity.web.Result;
import com.rc.service.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags="$(table.comment)")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
    return userService.getById(id);
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @PostMapping("/queryByPage")
    public Result queryUserByPage(PageParam pageParam){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        Map<String, String> queryMap = pageParam.getQueryMap();
        if (StringUtils.isNotBlank(queryMap.get("id"))){
            userQueryWrapper.like("id",queryMap.get("id"));
        }
        Page<User> userPage = userService.page(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), userQueryWrapper);
        return Result.SUCCESS(userPage);
    }

}
